<?php

namespace app\company\controller;

use app\common\exception\BaseException;
use app\common\service\annotation\NodeAnnotation;
use app\company\model\CompanyWarehouseArea as CompanyWarehouseAreaModel;
use app\common\controller\CompanyController;
use app\common\service\annotation\ControllerAnnotation;
use app\Request;
use think\App;
use think\response\Json;

/**
 * @ControllerAnnotation(title="仓库库区管理")
 */
class WarehouseArea extends CompanyController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model           = new CompanyWarehouseAreaModel();
        $this->each            = true;
        $this->joinAddTable    = 'location';
        $this->joinUpdateTable = 'location';
        $this->sort            = ['sort' => 'desc', 'id' => 'desc'];
    }

    public function each($list)
    {
        return $list->each(function ($item)
        {
            $item->warehouse                = $item->warehouse()->field('id,name')->find();
            $item->warehouse_location_list  = $item->location()->select();
            $item->warehouse_location_count = $item->location()->count();
            return $item;
        });
    }

    /**
     * @NodeAnnotation (title="新增")
     * @throws BaseException
     */
    public function add(Request $request): Json
    {
        $this->checkPost();
        $post = $request->post();
        $this->sceneValidate($post, 'add');
        $post['company_id'] = $this->companyId;
        try {
            $this->model->transaction(function () use ($post)
            {
                $this->model->save($post);
                if(!empty($post[$this->joinAddTable])){
                    $joinTables = array_filter(json_decode($post[$this->joinAddTable], true));
                    $tableField = array_map(function ($vo) use ($post)
                    {
                        $vo['company_id'] = $this->companyId;
                        return $vo;
                    }, $joinTables);
                    $this->model->location()->saveAll($tableField);
                }
            });
        } catch (\Exception $e) {
            return $this->error('新增失败:' . $e->getMessage());
        }
        return $this->success('新增成功');
    }

    /**
     * @NodeAnnotation (title="编辑")
     */
    public function edit(Request $request): Json
    {
        $this->checkPost();
        $post = $request->post();
        $rule = [
                'id' => 'require|integer'
        ];
        $this->validate($post, $rule);
        $row = $this->model->find($post['id']);
        if (!$row) return $this->error('数据不存在！');
        try {
            $this->model->transaction(function () use ($post, $row)
            {
                $row->save($post);
                if ($this->joinUpdateTable) {
                    $joinTables = array_filter(json_decode($post[$this->joinUpdateTable], true));
                    $tableField = array_map(function ($vo)
                    {
                        $vo['company_id'] = $this->companyId;
                        return $vo;
                    }, $joinTables);
                    $table      = $this->joinUpdateTable;
                    $this->model->$table()->saveAll($tableField);
                }
            });
        } catch (\Exception $e) {
            return $this->error('保存失败:' . $e->getMessage());
        }
        return $this->success('保存成功');
    }

}